package com.huifaapp.mall.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.huifaapp.mall.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by duanyu on 2018/1/17.
 */

public class Shoppingcart2popwinAdapter  extends BaseAdapter implements  View.OnClickListener {
    private List<String>list;
    private Context context;
    //当前Item被点击的位置
    private int currentItem;
    ArrayList<String> curr=new ArrayList<>();
    public void setCurrentItem(int currentItem) {
        this.currentItem = currentItem;
    }
    public Shoppingcart2popwinAdapter(List<String> list, Context context) {
        this.list = list;
        this.context = context;
    }
    public void setCurr(ArrayList<String> curr) {
        this.curr = curr;
    }


    @Override
    public void onClick(View v) {
        if (mMyItemclickListener != null) {
            mMyItemclickListener.itemclick(v, (Integer) v.getTag());
        }

    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View recyclerViewItem = LayoutInflater.from(context).inflate(R.layout.item_guige,parent, false);
       ViewHolder holder =new ViewHolder(recyclerViewItem);
        recyclerViewItem.setOnClickListener(this);
        holder.itme_popwin_classify.setText(list.get(position));
        holder.itemView.setTag(position);
//        //解决RecyclerView和checkBox问题
//        holder.itme_popwin_classify.setTag(position);
//        if (Tmall_homepage2Fragment.hmap.containsKey(position)) {
//            holder.itme_popwin_classify.setSelected(Tmall_homepage2Fragment.hmap.get(position));
//        } else {
//            holder.itme_popwin_classify.setSelected(false);//true or false 都可以，看实际需求
//        }



        if (currentItem == position) {
            Log.e("点击取的position选中",position+"");
            //如果被点击，设置当前TextView被选中
            holder.itme_popwin_classify.setSelected(true);
            holder.itme_popwin_classify.setBackgroundResource(R.drawable.shape_guige);
        } else {
            Log.e("","");
            Log.e("点击取的position未选中",position+"");
            //如果没有被点击，设置当前TextView未被选中
            holder.itme_popwin_classify.setSelected(false);
            holder.itme_popwin_classify.setBackgroundResource(R.drawable.shape_button_mm_guige);
        }
        if(curr.size()!=0) {
            for (int i = 0; i < curr.size(); i++) {
                if (curr.get(i).equals(list.get(position))) {
                    Log.e("详情无库存",curr+"      "+curr.get(i)+"    "+list.get(position));
                    holder.itme_popwin_classify.setSelected(true);
                    holder.itme_popwin_classify.setBackgroundResource(R.drawable.shape_button_mm_wukucun);
                }else{
                    if (currentItem == position) {
                        Log.e("点击取的position选中",position+"");
                        //如果被点击，设置当前TextView被选中
                        holder.itme_popwin_classify.setSelected(true);
                        holder.itme_popwin_classify.setBackgroundResource(R.drawable.shape_guige);
                    } else {
                        Log.e("","");
                        Log.e("点击取的position未选中",position+"");
                        //如果没有被点击，设置当前TextView未被选中
                        holder.itme_popwin_classify.setSelected(false);
                        holder.itme_popwin_classify.setBackgroundResource(R.drawable.shape_button_mm_guige);
                    }
                }
            }
        }
        return recyclerViewItem;
    }



    //itme点击接口回调
    public interface MypopItemclickListener {
        void itemclick(View view, int position);
    }
    public MypopItemclickListener mMyItemclickListener;

    public void setmMyItemclickListener(MypopItemclickListener mMyItemclickListener) {
        this.mMyItemclickListener = mMyItemclickListener;
    }
    class ViewHolder extends RecyclerView.ViewHolder {


        private final TextView itme_popwin_classify;

        public ViewHolder(View itemView) {
            super(itemView);
            itme_popwin_classify = (TextView) itemView.findViewById(R.id.itme_popwin_classify_gg);
        }
    }
}
